home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / insitem.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  276 lines

  1.  
  2. #include <windows.h>  
  3. #include "insitem.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "TabCtrl_InsertItem"; 
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105. HWND    hTab         = NULL;
  106. FARPROC pDefStatProc = NULL;
  107.  
  108. LRESULT CALLBACK StatProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static int nSel = 0;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_PAINT :
  115.             {
  116.                PAINTSTRUCT ps;
  117.  
  118.                BeginPaint( hWnd, &ps );
  119.  
  120.                // Determine which tab is displayed.
  121.                //..................................
  122.                nSel = TabCtrl_GetCurSel( hTab );
  123.  
  124.                // Paint the appropriate figure for the tab.
  125.                //..........................................
  126.                switch( nSel )
  127.                {
  128.                   case 0 : Ellipse( ps.hdc, 10, 10, 100, 100 );
  129.                            break;
  130.  
  131.                   case 1 : Rectangle( ps.hdc, 10, 10, 100, 100 );
  132.                            break;
  133.  
  134.                   case 2 : MoveToEx( ps.hdc, 10, 10, NULL );
  135.                            LineTo( ps.hdc, 100, 100 );
  136.                            MoveToEx( ps.hdc, 100, 10, NULL );
  137.                            LineTo( ps.hdc, 10, 100 );
  138.                            break;
  139.                }
  140.  
  141.                EndPaint( hWnd, &ps );
  142.             }
  143.             break;
  144.  
  145.       default :
  146.             return( (*pDefStatProc)(hWnd, uMsg, wParam, lParam) );
  147.    }
  148.  
  149.    return( FALSE );
  150. }
  151.  
  152.  
  153. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  154. {
  155. static RECT  rcDisp;
  156. static HWND  hStatic = NULL;
  157.  
  158.    switch( uMsg )
  159.    {
  160.       case WM_CREATE :
  161.               // Initialize and create the tab control and the static control.
  162.               //..............................................................
  163.               InitCommonControls();
  164.               hTab = CreateWindowEx( 0, WC_TABCONTROL, "", 
  165.                                      WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
  166.                                      0, 0, 10, 10, hWnd, 
  167.                                      (HMENU)101, hInst, NULL ); 
  168.  
  169.               hStatic = CreateWindowEx( 0, "STATIC", "", WS_CHILD | WS_VISIBLE, 
  170.                                       0, 0, 10, 10, hWnd, (HMENU)102, 
  171.                                       hInst, NULL );
  172.  
  173.               if ( hTab )
  174.               {
  175.                  TC_ITEM item;
  176.  
  177.                  SendMessage( hTab, WM_SETFONT, 
  178.                               (WPARAM)GetStockObject( DEFAULT_GUI_FONT ), 0 );
  179.  
  180.                  item.mask    = TCIF_TEXT;
  181.  
  182.                  item.pszText = "Circle";
  183.                  TabCtrl_InsertItem( hTab, 0, &item );
  184.  
  185.                  item.pszText = "Rectangle";
  186.                  TabCtrl_InsertItem( hTab, 1, &item );
  187.  
  188.                  item.pszText = "Lines";
  189.                  TabCtrl_InsertItem( hTab, 2, &item );
  190.               }
  191.  
  192.               // Subclass the static control to perform painting.
  193.               //.................................................
  194.               if ( hStatic )
  195.                  pDefStatProc = (FARPROC)SetWindowLong( hStatic, GWL_WNDPROC, (LONG)StatProc );
  196.               break;
  197.  
  198.       case WM_SIZE :
  199.               // Retrieve the client rect.
  200.               //..........................
  201.               GetClientRect( hWnd, &rcDisp );
  202.  
  203.               // Calculate the tab control display area.
  204.               //........................................
  205.               TabCtrl_AdjustRect( hTab, FALSE, &rcDisp );
  206.  
  207.               // Size the tab control.
  208.               //......................
  209.               MoveWindow( hTab, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE );
  210.               MoveWindow( hStatic, rcDisp.left, rcDisp.top, 
  211.                                    rcDisp.right-rcDisp.left, 
  212.                                    rcDisp.bottom-rcDisp.top, TRUE );
  213.               break;
  214.  
  215.       case WM_NOTIFY :
  216.               switch( ((NMHDR*)lParam)->code )
  217.               {
  218.                  case TCN_SELCHANGE :
  219.                         // The user selected a different tab.
  220.                         // Force the window to be painted.
  221.                         //...................................
  222.                         InvalidateRect( hTab, &rcDisp, TRUE );
  223.                         UpdateWindow( hTab );
  224.                         InvalidateRect( hStatic, NULL, TRUE );
  225.                         break;
  226.               }
  227.               break;
  228.  
  229.       case WM_COMMAND :
  230.               switch( LOWORD( wParam ) )
  231.               {
  232.                  case IDM_ABOUT :
  233.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  234.                         break;
  235.  
  236.                  case IDM_EXIT :
  237.                         DestroyWindow( hWnd );
  238.                         break;
  239.               }
  240.               break;
  241.       
  242.       case WM_DESTROY :
  243.               PostQuitMessage(0);
  244.               break;
  245.  
  246.       default :
  247.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  248.    }
  249.  
  250.    return( FALSE );
  251. }
  252.  
  253.  
  254. LRESULT CALLBACK About( HWND hDlg,           
  255.                         UINT message,        
  256.                         WPARAM wParam,       
  257.                         LPARAM lParam)
  258. {
  259.    switch (message) 
  260.    {
  261.        case WM_INITDIALOG: 
  262.                return (TRUE);
  263.  
  264.        case WM_COMMAND:                              
  265.                if (   LOWORD(wParam) == IDOK         
  266.                    || LOWORD(wParam) == IDCANCEL)    
  267.                {
  268.                        EndDialog(hDlg, TRUE);        
  269.                        return (TRUE);
  270.                }
  271.                break;
  272.    }
  273.  
  274.    return (FALSE); 
  275. }
  276.